package com.zhao.controller;

import com.zhao.pojo.Dept;
import com.zhao.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 提供rustful服务---接口
 *
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    //获取一些配置得信息，得到具体得微服务
    @Autowired
    private DiscoveryClient client;

    @PostMapping("/dept/add")
    public boolean addDept(@RequestBody Dept dept){
       return deptService.addDept(dept);
    }
    @GetMapping ("/dept/get/{id}")
    public Dept quertByid(@PathVariable("id") Long id){

        Dept dept = deptService.quertByid(id);
        if(id ==null){
            throw new RuntimeException("Faill");
        }

        return dept;
    }
    @GetMapping("/dept/list")
    public List queryDept(){
        return deptService.queryAll();
    }
    //注册进来得微服务获取一些信息,告诉别人你是谁，写的什么东东
    @GetMapping("/dept/discovery")
    public Object discovery(){
        //获得微服务得列表清单
        List<String> services = client.getServices();
        System.out.println("discovery"+services);

        //得到一个具体得微服务信息，通过具体得微服务id,applicaioinName;

        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");

        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost()+"\t"+
                     instance.getPort()+"\t"+
                      instance.getUri()+"\t"+
                       instance.getServiceId()+"\t"
            );
        }
        return this.client;
    }
}

